

var uploader = new plupload.Uploader({
    runtimes: 'html5,flash,silverlight,html4',
    browse_button: 'selectfiles',
    //runtimes : 'flash',
    container: document.getElementById('container'),
    //flash_swf_url : 'lib/plupload-2.1.2/js/Moxie.swf',
    //silverlight_xap_url : 'lib/plupload-2.1.2/js/Moxie.xap',
    url: host,
    filters: {
        mime_types: [ //只允许上传图片
            {title: "Image files", extensions: "jpg,gif,png,jpeg"}
        ],
        max_file_size: '200kb' //最大只能上传200kb的文件

    },
    multi_selection: false,//是否可以在文件浏览对话框中选择多个文件
    unique_names: true,//每个上传的文件生成一个唯一的文件名，并作为额外的参数post到服务器端，参数明为name,值为生成的文件名。
    multipart_params: {
        'Filename': '${filename}',
        'key': fileName + '${filename}',
        'policy': policy,
        'OSSAccessKeyId': accessid,
        'success_action_status': '200', //让服务端返回200,不然，默认会返回204
        'signature': signature,
    },
    init: {
        PostInit: function () {
            document.getElementById('ossfile').innerHTML = '';
        },
        FilesAdded: function (up, files) {
            plupload.each(files, function (file) {
                document.getElementById('ossfile').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ')<b></b>'
                    + '<div class="progress" ><div class="progress-bar" style="width: 0%"></div></div>'
                    + '</div>';
            });
            uploader.start();
        },
        UploadProgress: function (up, file) {
            var d = document.getElementById(file.id);
            d.getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
            var prog = d.getElementsByTagName('div')[0];
            var progBar = prog.getElementsByTagName('div')[0]
            progBar.style.width = 2 * file.percent + 'px';
            progBar.setAttribute('aria-valuenow', file.percent);
        },

        FileUploaded: function (up, file, info) {
            if (info.status >= 200 || info.status < 200) {
                document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = 'success';
                setTimeout(function () {
                    $("#ossfile").empty();
                }, 1000);
                var pathImg = host + "/" + fileName + file.name;
                $(".Js-imgView").attr("src", pathImg);
            } else {
                document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = info.response;
            }
        },
        Error: function (up, err) {
            swal("错误提示！","上传失败:图片大小不能超过200kb!","error");
            //document.getElementById('console').appendChild(document.createTextNode("\nError xml:" + err.response));
        }
    }
});

uploader.init();
